
#include "CEntity.hpp"
#include <iterator>
#include <algorithm>
using namespace std;


CEntity::CEntity(size_t aId, size_t aNDim)
    :_id(aId), _nDim(aNDim)
{
    normal = NULL;
    CG = new double[_nDim];

    boundType = 0;
}
CEntity::~CEntity()
{
    if(CG != NULL) delete[] CG;
}

void CEntity::init(CEntity** aPointList)
{
    for(size_t iDim = 0; iDim < _nDim; iDim++) CG[iDim] = 0.0;
    for(size_t iPoint = 0; iPoint < point.size(); iPoint++)
    {
        point[iPoint] = aPointList[iPoint]->getId();
        for(size_t iDim = 0; iDim < _nDim; iDim++) CG[iDim] += aPointList[iPoint]->CG[iDim];
    }

    for(size_t iDim = 0; iDim < _nDim; iDim++) CG[iDim] /= point.size();
    
}

int CEntity::findNeighborPoint(size_t aPoint)
{
    int tempNeighbor = -1;
    vector<size_t>::iterator iter = find(neighborPoint.begin(), neighborPoint.end(), aPoint);
    if(iter != neighborPoint.end())
    {
        tempNeighbor = *iter;
    }

    return tempNeighbor;
    

}

CPoint::CPoint(size_t aId, size_t aNDim)
    :CEntity(aId, aNDim)
{
    point.resize(0);
    face.resize(0);
    type = 1;
}

CPoint::~CPoint()
{
}

CLine::CLine(size_t aId, size_t aNDim)
    :CEntity(aId, aNDim)
{
    point.resize(2);
    face.resize(2);
    _nFacePoint = 1;
    type = 3;
}

CLine::~CLine()
{
}

CTriangle::CTriangle(size_t aId, size_t aNDim)
    :CEntity(aId, aNDim)
{
    point.resize(3);
    face.resize(3);
    _nFacePoint = 2;
    type = 5;
}

CTriangle::~CTriangle()
{
}

std::size_t* CTriangle::getFacePoint(std::size_t aFace)
{
    size_t* pointList = new size_t[2];
    if(aFace >=0 && aFace < face.size() - 1)
    {
        pointList[0] = point[aFace];
        pointList[1] = point[aFace+1];
    } else if(aFace == face.size() - 1)
    {
        pointList[0] = point[aFace];
        pointList[1] = point[0];
    } else
    {
        throw runtime_error("In function CTriangle::getFacePoint: the aFace is wrong!\n");
    }
    return pointList;
}

CRectangle::CRectangle(size_t aId, size_t aNDim)
    :CEntity(aId, aNDim)
{
    point.resize(4);
    face.resize(4);
    _nFacePoint = 2;
    type = 9;
}

CRectangle::~CRectangle()
{
}

std::size_t* CRectangle::getFacePoint(std::size_t aFace)
{
    size_t* pointList = new size_t[2];
    if(aFace >=0 && aFace < face.size() - 1)
    {
        pointList[0] = point[aFace];
        pointList[1] = point[aFace+1];
    } else if(aFace == face.size() - 1)
    {
        pointList[0] = point[aFace];
        pointList[1] = point[0];
    } else
    {
        throw runtime_error("In function CRectangle::getFacePoint: the aFace is wrong!\n");
    }
    return pointList;
}


